package com.example.exam;

import java.util.ArrayList;
import java.util.List;


interface Item1 {
    String getName();
    double getPrice();
}

// NoodleShop的另外一种实现，模仿BuilderPatternExample例子加了个Director而已
class BeefNoodle1 implements Item1 {
    private String size;

    public BeefNoodle1(String size) {
        this.size = size;
    }

    @Override
    public String getName() {
        return size + " Beef Noodle";
    }

    @Override
    public double getPrice() {
        switch (size) {
            case "Large": return 18;
            case "Medium": return 16;
            case "Small": return 14;
            default: throw new IllegalArgumentException("Invalid size");
        }
    }
}

class IntestineNoodle1 implements Item1 {
    private String size;

    public IntestineNoodle1(String size) {
        this.size = size;
    }

    @Override
    public String getName() {
        return size + " Pork Intestine Noodle";
    }

    @Override
    public double getPrice() {
        switch (size) {
            case "Large": return 20;
            case "Medium": return 18;
            case "Small": return 16;
            default: throw new IllegalArgumentException("Invalid size");
        }
    }
}

class BeefPie1 implements Item1 {
    @Override
    public String getName() {
        return "Beef Pie";
    }

    @Override
    public double getPrice() {
        return 10;
    }
}

class MilkTea1 implements Item1 {
    @Override
    public String getName() {
        return "Milk Tea";
    }

    @Override
    public double getPrice() {
        return 12;
    }
}

class Combo11 implements Item1 {
    @Override
    public String getName() {
        return "Combo 1";
    }

    @Override
    public double getPrice() {
        return 38;
    }
}

class Combo21 implements Item1 {
    @Override
    public String getName() {
        return "Combo 2";
    }

    @Override
    public double getPrice() {
        return 40;
    }
}


class Order1 {

    private List<Item1> items = new ArrayList<>();

    public void addItem(Item1 item1) {
        items.add(item1);
    }

    public void showItems() {
        for (Item1 item1 : items) {
            System.out.println(item1.getName() + ": " + item1.getPrice() + "元");
        }
    }

    public double getTotalPrice() {
        double total = 0;
        for (Item1 item1 : items) {
            total += item1.getPrice();
        }
        return total;
    }


}

class OrderBuilder1 {
    private Order1 order1;

    public OrderBuilder1() {
        order1 = new Order1();
    }

    public OrderBuilder1 addItem(Item1 item1) {
        order1.addItem(item1);
        return this;
    }

    public Order1 getOrder1() {
        return order1;
    }

    public void setOrder1(Order1 order1) {
        this.order1 = order1;
    }
}

class Director {
    private OrderBuilder1 orderBuilder1;

    public Director(OrderBuilder1 orderBuilder1) {
        this.orderBuilder1 = orderBuilder1;
    }

    public Order1 build() {
        return orderBuilder1.getOrder1();
    }
}


public class NoodleShop1 {
    public static void main(String[] args) {
        // 创建张三的订单
        OrderBuilder1 zhangSanOrderBuilder = new OrderBuilder1();
        zhangSanOrderBuilder.addItem(new Combo11())
                .addItem(new BeefPie1());
        Director zsdirector = new Director(zhangSanOrderBuilder);
        Order1 zhangSanOrder = zsdirector.build();

        System.out.println("张三的订单：");
        zhangSanOrder.showItems();
        System.out.println("张三需要付款: " + zhangSanOrder.getTotalPrice() + "元\n");

        // 创建李四的订单
        OrderBuilder1 liSiOrderBuilder = new OrderBuilder1();
        liSiOrderBuilder.addItem(new IntestineNoodle1("Medium"))
                .addItem(new MilkTea1())
                .addItem(new MilkTea1());
        Director lsdirector = new Director(liSiOrderBuilder);
        Order1 liSiOrder = lsdirector.build();

        System.out.println("李四的订单：");
        liSiOrder.showItems();
        System.out.println("李四需要付款: " + liSiOrder.getTotalPrice() + "元");
    }
}

